package com.example.imtao.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import com.example.imtao.R;
import com.example.imtao.adapter.MainHeaderAdAdapter;
import com.example.imtao.adapter.MainMenuAdapter;
import com.example.imtao.util.DataUtil;

public class MainFragment extends Fragment {
    private LinearLayout pointers;
    protected Handler handler = new Handler();
    protected  int [] icons={R.drawable.ad1,R.drawable.ad2,R.drawable.ad3};
    protected ViewPager2 pagers;
    protected ViewPager mVPagerHeaderAd;
    private int index = 1;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1)
            {
                pagers.setCurrentItem(index%3);
                index++;
            }
        }
    };
    protected  int [] menuIons={R.drawable.v1,R.drawable.v2,R.drawable.v3,
            R.drawable.v4,
            R.drawable.v5,
            R.drawable.v6,
            R.drawable.v7,
            R.drawable.v8};
    String [] menus;
    protected RecyclerView mRecyclerViewMenu;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main,container,false);

        pagers=view.findViewById(R.id.papers);
        pointers=view.findViewById(R.id.pointers);

        setViewPager();

        return  view;
    }

    private void setViewPager() {
        RecyclerView.Adapter adapter = new RecyclerView.Adapter() {
            @NonNull
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View v =LayoutInflater.from(getContext()).inflate(R.layout.adv_item,parent,false);
                return new ViewHolder(v);
            }

            @Override
            public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
                ViewHolder vh = (ViewHolder) holder;
                vh.v.setBackgroundResource(DataUtil.adv_imgs[position]);
            }

            @Override
            public int getItemCount() {
                return DataUtil.adv_imgs.length;
            }
        };


        pagers.setAdapter(adapter);

        pagers.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                for(int i=0 ; i<pointers.getChildCount();i++){
                    ImageView img = (ImageView) pointers.getChildAt(i);
                    img.setImageResource(R.drawable.dot_unselected);
                }
                ((ImageView)pointers.getChildAt(position)).setImageResource(R.drawable.dot_selected);
            }
        });


        new Thread(){
            @Override
            public void run(){
                super.run();
                while (true){
                    try {
                        Thread.sleep(3000);
                    }   catch (InterruptedException e){
                        e.printStackTrace();
                    }
                    Message message = new Message();
                    message.what = 1;
                    mHandler.sendMessage(message);
//                    pagers.setCurrentItem(index%4);
//                    index++;
                }
            }
        }.start();


    }


    class ViewHolder extends RecyclerView.ViewHolder{
        public View v;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            v = itemView;
        }
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        menus=this.getActivity().getResources().getStringArray(R.array.main_menu);

        mRecyclerViewMenu=(RecyclerView)getView().findViewById(R.id.recycleview_main_menu);
        MainHeaderAdAdapter adapter = new MainHeaderAdAdapter(getActivity(), DataUtil.getHeaderAddInfo(getActivity(),icons));


        mRecyclerViewMenu.setLayoutManager(new GridLayoutManager(getActivity(),4));
        MainMenuAdapter mainMenuAdapter=new MainMenuAdapter(getActivity(),DataUtil.getMainMenus(menuIons,menus));
        mRecyclerViewMenu.setAdapter(mainMenuAdapter);


        
    }
}
